package com.atguigu.lease.web.admin.controller;

import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.FeeKey;
import com.atguigu.lease.model.entity.FeeValue;
import com.atguigu.lease.web.admin.service.FeeKeyService;
import com.atguigu.lease.web.admin.service.FeeValueService;
import com.atguigu.lease.web.admin.vo.fee.FeeKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 李壮
 * @CreateTime: 2025-05-06
 * @Version: 1.0
 */


@RestController
@RequestMapping("/admin/fee")
@Tag(name = "房间杂费管理")
public class FeeController {

    @Autowired
    FeeKeyService feeKeyService;

    @Autowired
    FeeValueService feeValueService;


    @Operation(summary = "保存或更新杂费值")
    @PostMapping("/value/saveOrUpdate")
    public Result saveOrUpdateValue(@RequestBody FeeValue feeValue){
        feeValueService.saveOrUpdate(feeValue);
        return Result.ok();
    }

    @Operation(summary = "保存或更新杂费名称")
    @PostMapping("/key/saveOrUpdate")
    public Result saveOrUpdateKey(@RequestBody FeeKey feeKey){
        feeKeyService.saveOrUpdate(feeKey);
        return Result.ok();
    }

    @Operation(summary = "查询全部杂费名称和杂费值列表")
    @GetMapping("/list")
    public Result list(){
        List<FeeKeyVo> list = feeKeyService.getAll();
        return Result.ok(list);
    }

    @Operation(summary = "根据id删除杂费值")
    @DeleteMapping("/value/deleteById")
    public Result deleteValueById(@RequestParam Long id){
        feeValueService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id删除杂费名称")
    @DeleteMapping("/key/deleteById")
    public Result deleteKeyById(@RequestParam Long id){

        feeKeyService.removeById(id);

        LambdaQueryWrapper<FeeValue> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FeeValue::getFeeKeyId,id);
        feeValueService.remove(lambdaQueryWrapper);
        return Result.ok();
    }
}
